package com.binfenhulian.chinaenergy.ces.utils.number;

import java.math.BigDecimal;

public class DoubleUtil {
    public static double sum(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.add(bd2).doubleValue();
    }

    public static double sub(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }

    public static double mul(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.multiply(bd2).doubleValue();
    }

    public static double div(double d1, double d2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException();
        }
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.divide(bd2, scale, 4).doubleValue();
    }

    public static double round(double d1, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException();
        }
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal one = new BigDecimal("1");
        return bd1.divide(one, scale, 4).doubleValue();
    }

    public static double float2Double(float d1) {
        BigDecimal db1 = new BigDecimal(Float.toString(d1));
        return db1.doubleValue();
    }

    public static double mul2scale(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        double d = bd1.multiply(bd2).doubleValue();
        return round(d, 2);
    }

    public static String double2string(double d1) {
        BigDecimal bd = new BigDecimal(String.valueOf(d1));
        if (bd.scale() <= 2) {
            return String.format("%.2f", new Object[]{Double.valueOf(d1)});
        }
        return String.format("%." + bd.scale() + "f", new Object[]{Double.valueOf(d1)});
    }

    public static String double0stirng(double d1) {
        return String.format("%.0f", new Object[]{Double.valueOf(d1)});
    }

    public static String double2wan(double d1) {
        d1 = div(d1, 10000.0D, 2);
        BigDecimal bd = new BigDecimal(String.valueOf(d1));
        if (bd.scale() <= 2) {
            return String.format("%.2f", new Object[]{Double.valueOf(d1)});
        }
        return String.format("%." + bd.scale() + "f", new Object[]{Double.valueOf(d1)});
    }

    public static String proportion(double d1, double d2) {
        return String.format("%.0f", new Object[]{Double.valueOf(div(d1 * 100.0D, d2, 3))});
    }

    public static String proportion2(double d1, double d2) {
        return String.format("%.2f", new Object[]{Double.valueOf(div(d1 * 100.0D, d2, 3))});
    }

    public static double jine(int jine) {
        return div(jine, 10.0D, 2);
    }

    public static double poundage(double d1, double d2) {
        double poundage = mul2scale(d1, d2);
        return poundage == 0.0D ? 0.01D : poundage;
    }

    public static double rate(int num1,int num2) {
        if (num2 == 0) {
            return 0.0d;
        }
        BigDecimal bg = new BigDecimal((float)num1/num2 * 100);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public static double keep2(double num){
        BigDecimal bg = new BigDecimal(num);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }


}